<?php 
if(!isset($_SESSION))
    session_start();
class checkout extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('smarty3');
            $this->smarty=new CI_Smarty3();$this->smarty->error_reporting = E_ALL & ~E_NOTICE;
            $_SESSION["DRegion"]=isset($_SESSION["DRegion"])?$_SESSION["DRegion"]:"";
            $_SESSION["DRegionName"]=isset($_SESSION["DRegionName"])?$_SESSION["DRegionName"]:"Toàn Quốc";
            require APPPATH .'controllers/Excution.php';
            require( APPPATH . 'controllers/APanel/mailler.php');
            $Excution = new Excution();
            $Excution->Init();
        }
        
        public function index(){
            $ID=$this->input->post("ID");
            $Amount=(int)$this->input->post("Amount");
            $Name=$this->input->post("Name");
            $Name=ucwords(strtolower($Name));
            $Phone=$this->input->post("Phone");
            $Address=$this->input->post("Address");
            $Note=$this->input->post("Note");
            
            $this->load->model('APanel/product_model');
            $this->product_model=new product_model();
            $tmp=$this->product_model->getByID($ID);
            $sCusID=isset($_SESSION["D-USER"]["CustomerID"])?$_SESSION["D-USER"]["CustomerID"]:"0";
            if(isset($tmp[0])){
                $oProduct=$tmp[0];
                $OrderDetail=array(
                    "Product"=>array(
                        "ProductID"=>$oProduct["ProductID"],
                        "ProductName"=>$oProduct["ProductName"],
                        "UnitsOnOrder"=>$oProduct["UnitsOnOrder"],
                        "Amount"=>$oProduct["Amount"],
                        "Sold"=>$oProduct["Sold"]
                    ),
                    "ShipInfo"=>array(
                        "ShipName"=>$Name,
                        "ShipPhone"=>$Phone,
                        "ShipNote"=>$Note,
                        "ShipCash"=>0
                    ),
                    "PayInfo"=>array(
                        "PayType"=>"",
                        "Address"=>$Address,
                        "Region"=>"","Co"=>"","subCo"=>"",
                        "strShipAddress"=>$Address
                    )
                );
                $oParams=array(
                    "ProductID"=>$ID,
                    "UnitsOnOrder"=>$oProduct["UnitsOnOrder"],
                    "Amount"=>$Amount,
                    "CustomerID"=>$sCusID,
                    "ShipName"=>$Name,
                    "ShipAddress"=>$this->input->post("Address"),
                    "Monney"=>$Amount*$oProduct["UnitsOnOrder"],
                    "Status"=>"New",
                    "OrderDate"=>date("Y-m-d"),
                    "OrderDetail"=>  json_encode($OrderDetail)
                );
                $this->load->model('APanel/order_model');
                $this->order_model=new order_model();
                $rs=$this->order_model->insert($oParams);
                if($rs){
                    $code=1;
                    $msg="<b>Đặt hàng thành công.</b> Chúng tôi sẽ liên hệ, giao hàng trong thời gian sớm nhất.";
                    $sBody=@$this->getBody($oProduct, $Name, $Phone, $Address, $Note, $Amount);
                    $sSubject="[Thông báo] ".date("H:i d/m/Y")." - Đơn đặt hàng mới từ Dealgiadung.com - ".time();
                    $this->load->model('APanel/setting_model');
                    $this->setting_model=new setting_model();
                    $tmp=@$this->setting_model->getByKey("check-out-to-email");
                    if(isset($tmp[0])){
                        $mailler = new mailler();
                        $emails=  @explode(",", $tmp[0]->Value);
                        if(count($emails)>0)foreach ($emails as $e){
                            $e=  @trim($e);
                            if(@preg_match("/^[0-9a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $e) !== 0){
                                @$mailler->MailTo($e, $e, $sSubject, $sBody);
                            }
                        }
                    }
                }else{
                    $code=-101;
                    $msg="Hệ thống đang bận. vui lòng thử lại.";
                }
            }else{
                $code=-404;
                $msg="Không tìm thấy sản phẩm, hoặc sản phẩm đã ngưng cung cấp.";
            }
            
            $result=array("code"=>$code,"msg"=>$msg);
            echo json_encode($result);
        }
        public function deal($alias="")
	{
            $Configs["U"]["avata"]="default";
            $Configs["U"]["email"]="";
            if(isset($_SESSION["D-USER"]))$Configs["U"]=$_SESSION["D-USER"];
            $data=null;
            
            $this->load->model('APanel/content_model');
            $this->content_model=new content_model();
            $tmp=$this->content_model->getByID("42");
            if(isset($tmp[0]))
                $data["oNote1"]=$tmp[0];
            
            $tmp=$this->content_model->getByID("43");
            if(isset($tmp[0]))
                $data["oNote2"]=$tmp[0];
            
            $this->load->model('APanel/product_model');
            $this->product_model=new product_model();
            $tmp=$this->product_model->getByAlias($alias);
            if(isset($tmp[0]))
                $data["oProduct"]=$tmp[0];
            
            $this->load->model('APanel/region_model');
            $this->region_model=new region_model();
            $tmp=$this->region_model->getCitys();
            if(isset($tmp[0]))
                $data["oCitys"]=$tmp;
            
            $this->smarty
                    ->assign('data', $data)
                    ->assign('_SESSION', $_SESSION)
                    ->assign('Configs', $Configs)
                    ->display  ("templates/07_checkout");
	}
        private function getBody($oProduct,$Name="",$Phone="",$Address="",$Note="",$Amount=1){
            return "
                <div style='
                    font-family: tahoma;font-size: 11px;
                    width: 600px;
                    border: 1px solid #ccc;
                    '>
                    <div style='line-height: 28px;border-bottom: 1px solid #ccc;padding-left: 20px;font-family: tahoma;font-size: 11px;'>
                        [ Thông báo ] Bạn có một đơn hàng mới.
                    </div>
                    <div style='padding: 20px'>
                        <div style='line-height: 28px;font-family: tahoma;font-size: 11px;'>
                            Thông tin sản phẩm
                        </div>
                        <table style='display: inline-block;border-collapse: collapse;font-family: tahoma;font-size: 11px;width: 100%'>
                            <tr style='display: table-row;'>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;font-weight:bold'>Sản Phẩm</td>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;font-weight:bold'>Đơn giá</td>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;font-weight:bold'>Số Lượng</td>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;font-weight:bold'>Thành Tiền</td>
                            </tr>
                            <tr style='display: table-row;'>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;'>".$oProduct["ProductName"]."</td>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;'>".number_format($oProduct["UnitsOnOrder"], 0, ',', '.')."<sup>đ</sup></td>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;'>".$Amount."</td>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;'>".number_format(($Amount*$oProduct["UnitsOnOrder"]), 0, ',', '.')."<sup>đ</sup></td>
                            </tr>
                        </table>
                        <div style='line-height: 28px;font-family: tahoma;font-size: 11px;padding-top: 20px'>
                            Thông tin đặt hàng
                        </div>
                        <table style='display: inline-block;border-collapse: collapse;font-family: tahoma;font-size: 11px;width: 100%'>
                            <tr style='display: table-row;'>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;font-weight:bold;width: 100px'>Họ và tên</td>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;'>".$Name."</td>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;font-weight:bold;width: 100px'>Điện thoại</td>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;'>".$Phone."</td>
                            </tr>
                            <tr style='display: table-row;'>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;font-weight:bold;vertical-align: top'>Địa chỉ</td>
                                <td colspan='3' style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;'>".$Address."</td>
                            </tr>
                            <tr style='display: table-row;'>
                                <td style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;font-weight:bold;vertical-align: top'>Ghi chú</td>
                                <td colspan='3' style='border-bottom: 1px dotted #D7D7D7;display: table-cell;padding: 4px 8px;'><pre style='white-space: pre-wrap;white-space: -moz-pre-wrap;white-space: -pre-wrap;white-space: -o-pre-wrap;word-wrap: break-word;font-family: tahoma;font-size: 11px;'>".$Note."</pre></td>
                            </tr>
                        </table>
                        <div style='line-height: 28px;font-family: tahoma;font-size: 11px;padding-top: 20px'>
                            Thông báo được tự động gửi khi có đơn hàng mới.
                        </div>
                    </div>
                </div>";
        }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */